<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="房间号" prop="dormitoryNumber">
        <el-input
          v-model="queryParams.dormitoryNumber"
          placeholder="请输入房间号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['zsw:dormitory_info:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['zsw:dormitory_info:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['zsw:dormitory_info:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['zsw:dormitory_info:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="dormitory_infoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="房间号" align="center" prop="dormitoryNumber" />
      <el-table-column label="宿舍类型" align="center" prop="dormitoryType" />
      <el-table-column label="宿舍状态" align="center" prop="dormitoryStatus">
      <template slot-scope="scope">
          <dict-tag :options="dict.type.zsw_dormitory_status" :value="scope.row.dormitoryStatus"/>
      </template>
      </el-table-column>
      <el-table-column label="宿舍容量" align="center" prop="dormitoryMax" />
      <el-table-column label="在住人数" align="center" prop="correntPeople" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['zsw:dormitory_info:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['zsw:dormitory_info:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改宿舍信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="房间号" prop="dormitoryNumber">
          <el-input v-model="form.dormitoryNumber" placeholder="请输入房间号" />
        </el-form-item>
        <el-form-item label="宿舍类型" prop="dormitoryType">
          <el-input v-model="form.dormitoryType" placeholder="请输入宿舍类型" />
        </el-form-item>
        <el-form-item label="宿舍状态" prop="dormitoryStatus">
          <el-radio-group v-model="form.dormitoryStatus">
            <el-radio
              v-for="dict in dict.type.zsw_dormitory_status"
              :key="dict.label"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="宿舍容量" prop="dormitoryMax">
          <el-input-number v-model="form.dormitoryMax" controls-position="right" :min="0" :max="6" />
        </el-form-item>
        <el-form-item label="在住人数" prop="correntPeople">
          <el-input-number v-model="form.correntPeople" controls-position="right" :min="0" :max="6" />
        </el-form-item>
        <el-divider content-position="center">学生信息信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddCzjStudents">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteCzjStudents">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="czjStudentsList" :row-class-name="rowCzjStudentsIndex" @selection-change="handleCzjStudentsSelectionChange" ref="czjStudents">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="学号" prop="studentNumber" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.studentNumber" placeholder="请输入学号" />
            </template>
          </el-table-column>
                 <el-table-column label="全名" prop="name" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.name" placeholder="请输入全名" />
            </template>
          </el-table-column>
          <el-table-column label="联系方式" prop="phoneNumber" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.phoneNumber" placeholder="请输入联系方式" />
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog :title="title" :visible.sync="openaddstudent" width="400px" :style="dialogStyle">
      <el-form ref="form" :model="form" label-width="80px">
      <el-form-item label="学号">
        <el-input
          v-model="studentNumber"
          type="text"
          placeholder="请输入学号"
        />
        </el-form-item>
      </el-form>
        <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="findstudentmsg">确 定</el-button>
        <el-button @click="canceladdstudent">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listDormitory_info, getDormitory_info, delDormitory_info, addDormitory_info, updateDormitory_info,findstudentbystudentNumber,delindormitory} from "@/api/zsw/dormitory_info";

export default {
  name: "Dormitory_info",
  dicts:['zsw_dormitory_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      //选中学生id
      sids: [],
      // 子表选中数据
      checkedCzjStudents: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 宿舍信息表格数据
      dormitory_infoList: [],
      // 学生信息表格数据
      czjStudentsList: [],
      studentNumber:'',
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      //添加学生弹出层
      openaddstudent:false,
      dialogStyle: {
        top: '40%'
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        dormitoryNumber: null,
        studentId: null,
        dormitoryManagerId: null,
        dormitoryType: null,
        dormitoryStatus: null,
        dormitoryMax: null,
        correntPeople: null,
        updtaeTime: null,
        deleteBy: null,
        deleteTime: null,
        deleteFlag: null
      },
      // 表单参数
      form: {
      },
      // 表单校验
      rules: {
        dormitoryNumber: [
          { required: true, message: "房间号不能为空", trigger: "blur" }
        ],
        dormitoryType: [
          { required: true, message: "宿舍类型不能为空", trigger: "blur" }
        ],
        dormitoryStatus: [
          { required: true, message: "宿舍状态不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询宿舍信息列表 */
    getList() {
      this.loading = true;
      listDormitory_info(this.queryParams).then(response => {
        this.dormitory_infoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    canceladdstudent(){
      this.openaddstudent=false;
      this.studentNumber='';
    },
    // 表单重置
    reset() {
      this.form = {
        dormitoryId: null,
        dormitoryNumber: null,
        studentId: null,
        dormitoryManagerId: null,
        dormitoryType: null,
        dormitoryStatus: null,
        dormitoryMax: null,
        correntPeople: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updtaeTime: null,
        deleteBy: null,
        deleteTime: null,
        deleteFlag: null,
        czjStudentsList: null
      };
      this.studentNumber='';
      this.czjStudentsList=[];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.dormitoryId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加宿舍信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const dormitoryId = row.dormitoryId || this.ids
      getDormitory_info(dormitoryId).then(response => {
        this.form = response.data;
        this.czjStudentsList = response.data.czjStudentsList;
        this.open = true;
        this.title = "修改宿舍信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.dormitoryId != null) {
            if(this.czjStudentsList.length>this.form.dormitoryMax){
              this.$modal.msgError("该宿舍已满，请重新分配人员");
              return
            }
            updateDormitory_info(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            this.form.czjStudentsList = this.czjStudentsList;
            if(this.czjStudentsList.length>this.form.dormitoryMax){
              this.$modal.msgError("该宿舍已满，请重新分配人员");
              return
            }
            addDormitory_info(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const dormitoryIds = row.dormitoryId || this.ids;
      this.$modal.confirm('是否确认删除宿舍信息编号为"' + dormitoryIds + '"的数据项？').then(function() {
        return delDormitory_info(dormitoryIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
	/** 学生信息序号 */
    rowCzjStudentsIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 学生信息添加按钮操作 */
    handleAddCzjStudents() {
      this.title="添加学生"
      this.openaddstudent=true;
    },
    /** 学生信息删除按钮操作 */
    handleDeleteCzjStudents() {
      if (this.checkedCzjStudents.length == 0) {
        this.$modal.msgError("请先选择要删除的学生信息数据");
      } else {
        const czjStudentsList = this.czjStudentsList;
        const checkedCzjStudents = this.checkedCzjStudents;
        this.czjStudentsList = czjStudentsList.filter(function(item) {
          return checkedCzjStudents.indexOf(item.index) == -1
        });
        delindormitory(this.sids);
      }
    },
    /** 复选框选中数据 */
    handleCzjStudentsSelectionChange(selection) {
      this.checkedCzjStudents = selection.map(item => item.index);
      this.sids=selection.map(item => item.studentId);
      console.log(this.sids);
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('zsw/dormitory_info/export', {
        ...this.queryParams
      }, `dormitory_info_${new Date().getTime()}.xlsx`)
    },
    findstudentmsg(){
        findstudentbystudentNumber(this.studentNumber).then(
              response => {
              console.log(response.data);
              this.$modal.msgSuccess("增加成功");
              this.openaddstudent = false;
              let obj = {};
              obj.name = response.data.name;
              obj.studentNumber = response.data.studentNumber;
              obj.phoneNumber = response.data.phoneNumber;
              this.czjStudentsList.push(obj);
              }
        )
    }
  }
};
</script>
